#include "cv.h"
#include "highgui.h"
#include <stdio.h>

// A Simple Video Capture Framework
int main() {

	  int height,width,step,channels;
	  uchar *data;
	  int i,j,k;
	
  CvCapture* capture = cvCaptureFromAVI( "Pure_Pwnage_103.avi" );
  if( !capture ) {
    fprintf( stderr, "ERROR: capture is NULL \n" );
    getchar();
    return -1;
  }

  // Create a window in which the captured images will be presented
  cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );

  // Show the image captured from the camera in the window and repeat
  while( 1 ) {
    // Get one frame
    IplImage* frame = cvQueryFrame( capture );
    if( !frame ) {
      fprintf( stderr, "ERROR: frame is null...\n" );
      getchar();
      break;
    }
    // get the image data
    height    = frame->height;
    width     = frame->width;
    step      = frame->widthStep;
    channels  = frame->nChannels;
    data      = (uchar *)frame->imageData;
    
    // invert the image
    for(i=0;i<height;i++) for(j=0;j<width;j++) for(k=0;k<channels;k++)
      data[i*step+j*channels+k]=255-data[i*step+j*channels+k];
    
    cvShowImage( "mywindow", frame );
    // Do not release the frame!

    //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
    //remove higher bits using AND operator
    if( (cvWaitKey(10) & 255) == 27 ) break;
  }

  // Release the video, housekeeping
  cvReleaseCapture( &capture );
  cvDestroyWindow( "mywindow" );
  return 0;
}
